﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using log4net;
using System.Text;
using AMebooking.NetUtils;
using System.Globalization;
using AMebooking.NetUtils.Settings;
using AMebooking.Models;
using System.Collections.Generic;
using AMebooking.Database;
using Newtonsoft.Json;

namespace AMebooking.Service
{
    public partial class ReservationTimeServices : System.Web.UI.Page
    {
        protected static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        protected static readonly StringBuilder sb = new StringBuilder();

        protected void Page_Load(object sender, EventArgs e)
        {
            var defaultDatetimeFormat = Constants.DEFAULT_DATE_FORMAT;
            var startDate = StringUtils.DefaultIfBlank(Request.QueryString["startDate"], DateTime.Now.ToString(defaultDatetimeFormat));
            var endDate = StringUtils.DefaultIfBlank(Request.QueryString["endDate"], DateTime.Now.ToString(defaultDatetimeFormat));
            var licno = StringUtils.DefaultIfBlank(Request.QueryString["licno"], "");
            var reservationNo = StringUtils.DefaultIfBlank(Request.QueryString["reservationNo"], "");
            var deptid = StringUtils.DefaultIfBlank(Request.QueryString["deptid"], "");
            var smanid = StringUtils.DefaultIfBlank(Request.QueryString["smanid"], "");
            var siteid = StringUtils.DefaultIfBlank(Request.QueryString["siteid"], "");

            var startDateTime = DateTime.ParseExact(startDate, defaultDatetimeFormat, CultureInfo.InvariantCulture);
            var endDateTime = DateTime.ParseExact(endDate, defaultDatetimeFormat, CultureInfo.InvariantCulture);


            endDateTime = endDateTime.AddDays(1);
            var workoderNo = -1;
            if (!string.IsNullOrEmpty(reservationNo))
                workoderNo = Int32.Parse(reservationNo);

            List<TimeReservation> ret = new List<TimeReservation>();
            List<TimeReservation> input = new List<TimeReservation>();

            input = DBAccess.getWorkOrders(deptid, smanid, startDateTime, endDateTime, workoderNo, licno, siteid);

            ret = DBAccess.getResourcesReservationObj(input, siteid);

            var json = JsonConvert.SerializeObject(ret);

            sb.Length = 0;
            sb.Append("startDate=")
                .Append(startDate)
                .Append("endDate=")
                .Append(endDate)
                .Append(Environment.NewLine)
                .Append(json);
            logger.Debug(sb);

            Page.Response.ContentType = "application/json";
            Page.Response.Write(json);
            Page.Response.End();

        }
    }
}
